﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using ElCharro01.Models;

namespace ElCharro01.Areas.User.Controllers
{
    public class CartDetailController : Controller
    {
        private Entities db = new Entities();

        public ActionResult IncreaseQuantity(int cartDetailId)
        {
            CartDetail cartDetail = db.CartDetailSet.Single(c => c.ID == cartDetailId);

            // aumento la quantità ordinata se non supero la disponibilità in stock 
            if (cartDetail.Quantita + 1 <= cartDetail.Variant.InStock)
            {
                cartDetail.Quantita = cartDetail.Quantita + 1;
                cartDetail.Totale = cartDetail.Totale + cartDetail.Variant.PrezzoUnitario; // aggiorno il totale per il cart detail
                db.ObjectStateManager.ChangeObjectState(cartDetail, EntityState.Unchanged);
                db.CartDetailSet.Attach(cartDetail);
                db.ObjectStateManager.ChangeObjectState(cartDetail, EntityState.Modified);
                db.SaveChanges();

                //aggiorno il totale del carrello
                Cart Cart = cartDetail.Cart;
                Cart.Totale = cartDetail.Cart.Totale + cartDetail.Variant.PrezzoUnitario;
                db.ObjectStateManager.ChangeObjectState(Cart, EntityState.Unchanged);
                db.CartSet.Attach(Cart);
                db.ObjectStateManager.ChangeObjectState(Cart, EntityState.Modified);
                db.SaveChanges();

            }
            return RedirectToAction("Details","Cart");
        }

        public ActionResult DecreaseQuantity(int cartDetailId)
        {
            CartDetail cartDetail = db.CartDetailSet.Single(c => c.ID == cartDetailId);

            // aumento la quantità ordinata se non supero la disponibilità in stock 
            if (cartDetail.Quantita - 1 > 0)
            {
                cartDetail.Quantita = cartDetail.Quantita - 1;
                cartDetail.Totale = cartDetail.Totale - cartDetail.Variant.PrezzoUnitario;
                db.ObjectStateManager.ChangeObjectState(cartDetail, EntityState.Unchanged);
                db.CartDetailSet.Attach(cartDetail);
                db.ObjectStateManager.ChangeObjectState(cartDetail, EntityState.Modified);
                db.SaveChanges();

                //aggiorno il totale del carrello
                Cart Cart = cartDetail.Cart;
                Cart.Totale = cartDetail.Cart.Totale - cartDetail.Variant.PrezzoUnitario;
                db.ObjectStateManager.ChangeObjectState(Cart, EntityState.Unchanged);
                db.CartSet.Attach(Cart);
                db.ObjectStateManager.ChangeObjectState(Cart, EntityState.Modified);
                db.SaveChanges();

                return RedirectToAction("Details", "Cart");
            }
            else
               return RedirectToAction("Delete", new { cartDetailId = cartDetailId });
           
        }


        public ActionResult Delete(int cartDetailId) 
        {

            CartDetail cartDetail = db.CartDetailSet.Single(c => c.ID == cartDetailId);

            //aggiorno il totale del carrello
            Cart Cart = cartDetail.Cart;
            Cart.Totale = cartDetail.Cart.Totale - ( cartDetail.Variant.PrezzoUnitario * cartDetail.Quantita );
            db.ObjectStateManager.ChangeObjectState(Cart, EntityState.Unchanged);
            db.CartSet.Attach(Cart);
            db.ObjectStateManager.ChangeObjectState(Cart, EntityState.Modified);
            db.SaveChanges();

            //cancello il cart detail
            db.CartDetailSet.DeleteObject(cartDetail);
            db.SaveChanges();
            return RedirectToAction("Details", "Cart");
        }


    }
}
